package oktx.config;

import oktx.thread.NarayanaTxThreadDetach;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * Created by dave on 18-5-19 下午5:57.
 */
@Configuration
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@Import({ApplicationContextHolder.class, TransactionBeanPostProcessor.class,
    LocalTxTerminator.class, NarayanaTxThreadDetach.class})
public class OktxAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(ThreadPoolTaskExecutor.class)
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
        executor.setQueueCapacity(512);
        return executor;
    }
}
